﻿using System;
using I32 = Reign.Core.MathI32;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.GamerServices;

#if WP7
using Microsoft.Phone.Shell;
using Microsoft.Advertising.Mobile.Xna;
using Microsoft.Xna.Framework.Content;
#endif

namespace Reign.Core
{
	#if WP7
	// This is a TEMP class for a game im making, and I will be removing WP7 code from Reign to replace it with WP8
	public class GameDummy : IDisposable
	{
		public GraphicsDevice GraphicsDevice;
		public ContentManager Content;

		public GameDummy() {}

		public void init(Application application, int width, int height, ApplicationOrientations orientation) {}

		public void Run() {}

		public void Dispose() {}
	}
	#endif

	public class XNAGame : Game
	{
		#region Properties
		private Application application;
		private GraphicsDeviceManager graphics;

		#if WP7
		private DrawableAd drawableAd;
		#endif

		public bool SimulateTrailMode
		{
			get
			{
				return Guide.SimulateTrialMode;
			}
			set
			{
				Guide.SimulateTrialMode = value;
			}
		}

		public bool TrailMode
		{
			get {return Guide.IsTrialMode;}
		}
		#endregion

		#region Constructors
		protected void init(Application application, int width, int height, ApplicationOrientations orientation)
		{
			this.application = application;

			try
			{
				#if XNA && !WP7
				graphics = new GraphicsDeviceManager(this);
				graphics.PreparingDeviceSettings += graphics_PreparingDeviceSettings;
				graphics.SynchronizeWithVerticalRetrace = true;

				#if XBOX360 || WP7
				graphics.PreferredBackBufferWidth = width == 0 ? GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width : width;
				graphics.PreferredBackBufferHeight = height == 0 ? GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height : height;
				#else
				if (width == 0 || height == 0) Debug.ThrowError("Application", "Width or Height can not be 0");
				graphics.PreferredBackBufferWidth = width;
				graphics.PreferredBackBufferHeight = height;
				#endif

				#if WP7
				var requiredOrientation = (orientation == ApplicationOrientations.Landscape) ? DisplayOrientation.LandscapeLeft : DisplayOrientation.Portrait;
				graphics.SupportedOrientations = requiredOrientation;
				graphics.IsFullScreen = true;
				
				TargetElapsedTime = TimeSpan.FromTicks(333333/2);
				InactiveSleepTime = TimeSpan.FromSeconds(1);
				PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
				OS.CurrentApplication = this;
				#else
				var gsc = new GamerServicesComponent(this); 
				Components.Add(gsc);
				#endif

				Content.RootDirectory = "Content";
				#endif
			}
			catch (Exception e)
			{
				application.Close();
				throw e;
			}
		}

		private void graphics_PreparingDeviceSettings(object sender, PreparingDeviceSettingsEventArgs e)
		{
			e.GraphicsDeviceInformation.PresentationParameters.PresentationInterval = PresentInterval.One;
		}
		#endregion

		#region Methods
		protected override void LoadContent()
		{
			application.frameSize = new I32.Vector2(GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight);
			application.shown();
		}

		protected override void UnloadContent()
		{
			application.closing();
		}
		
		protected override void Update(GameTime gameTime)
		{
			application.update();
			base.Update(gameTime);
		}

		protected override void Draw(GameTime gameTime)
		{
			application.render();
			base.Draw(gameTime);
		}
		#endregion
	}
}
